﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
//using System.Xml.Linq;
using KPIS.GERP.GHRMS.APP.Classes;
using Telerik.Web.UI;
using KPIS.GERP.GHRMS.BLL;
using KPIS.GERP.GHRMS.IBLL;
using KPIS.GERP.GHRMS.MODEL;
using KPIS.GERP.GHRMS.DAL;
using System.Collections.Generic;

namespace KPIS.GERP.GHRMS.APP.App_UI.Forms
{
    public partial class TR_Lecturers : System.Web.UI.Page
    {
        private SsMng ssMng = new SsMng();
        private IMasNamePrefixBLL NamePreBLL = new MasNamePrefixBLL();
        private IMasProvinceBLL proBLL = new MasProvinceBLL();
        private IMasAmphurBLL AmpBLL = new MasAmphurBLL();
        private IMasDistrictBLL disBLL = new MasDistrictBLL();
        private IEmployeeBLL EmpBLL = new EmployeeBLL();
        private ITrainingLecturersBLL LecturersBLL = new TrainingLecturersBLL();
        private ConvertNullable cn = new ConvertNullable();
        private Main main = new Main();
   
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {                
                LoadDataGrid();
                //LoadEmployeeList();
                LoadProvince();
                LoadPrefix();
                
            }
        }
        public void LoadDataGrid()
        {

            grdTRLecturers.DataSource = LecturersBLL.GetFull();
            grdTRLecturers.DataBind();

        }
        protected void grdTRLecturers_ItemCommand(object o, GridCommandEventArgs e)
        {
            if (e.CommandName == "AddData")
            {                
                UpdateUI("AddNew");

            }
            else if (e.CommandName == "Delete")
            {
                string TrainerSeq = grdTRLecturers.Items[e.Item.ItemIndex]["trainer_seq"].Text;
                int UserID = ssMng.EmpSeqI;
                DateTime dtNow = DateTime.Now;

                TrainingLecturersInfo info = new TrainingLecturersInfo();
                info.trainer_seq = Convert.ToInt32(TrainerSeq);
                info.updated_by = UserID;
                info.updated_when = dtNow;

               LecturersBLL.Delete(info);
            }
            else if (e.CommandName == "DataEdit")
            {
                UpdateUI("Update");

                if (grdTRLecturers.Items[e.Item.ItemIndex]["prov_seq"].Text  != "&nbsp;")
                {
                LoadDistrict("", grdTRLecturers.Items[e.Item.ItemIndex]["prov_seq"].Text);
                }

                txtTrainerSeq.Text = grdTRLecturers.Items[e.Item.ItemIndex]["trainer_seq"].Text;
                txtCitizen.Text = grdTRLecturers.Items[e.Item.ItemIndex]["citizen"].Text;
                cbPrefix.SelectedValue = grdTRLecturers.Items[e.Item.ItemIndex]["name_prefix_seq"].Text;
                txtName.Text = grdTRLecturers.Items[e.Item.ItemIndex]["first_name_local"].Text;
                txtSerName.Text = grdTRLecturers.Items[e.Item.ItemIndex]["last_name_local"].Text;
                string strAdd = grdTRLecturers.Items[e.Item.ItemIndex]["address"].Text;
                txtAddress.Text = main.WebString(strAdd);
                cmbProvince.SelectedValue = grdTRLecturers.Items[e.Item.ItemIndex]["prov_seq"].Text;
                cmbDistrict.SelectedValue = grdTRLecturers.Items[e.Item.ItemIndex]["district_seq"].Text;               
                string strPosC = grdTRLecturers.Items[e.Item.ItemIndex]["postal_code"].Text;
                txtPostCode.Text = main.WebString(strPosC);
                string strTel = grdTRLecturers.Items[e.Item.ItemIndex]["tel"].Text;
                txtTelNum.Text = main.WebString(strTel);
                txtEmail.Text = main.WebString(grdTRLecturers.Items[e.Item.ItemIndex]["email"].Text);
                txtWebSite.Text = main.WebString(grdTRLecturers.Items[e.Item.ItemIndex]["website"].Text);
                string strActive = grdTRLecturers.Items[e.Item.ItemIndex]["active"].Text;
                //string strPerson = grdTRLecturers.Items[e.Item.ItemIndex]["emp_seq"].Text;
                if (strActive == "1")
                { cboxStatus.Checked = true; }
                else
                { cboxStatus.Checked = false; }
                //if (strPerson.Length < 0)
                //{
                //    rbExperson.Checked = false;
                //    rbInperson.Checked = true;
                //    cbEmployeeList.Visible = true;
                //    cbEmployeeList.SelectedValue = grdTRLecturers.Items[e.Item.ItemIndex]["emp_seq"].Text;

                //}
                //else
                //{
                //    rbExperson.Checked = true;
                //    rbInperson.Checked = false;
                //    cbEmployeeList.Visible = false;
                //    cbEmployeeList.SelectedValue = "";
                //}

            }

            grdTRLecturers.DataSource = LecturersBLL.GetFull();
            grdTRLecturers.DataBind();
        }
        protected void btnSave_Click(object sender, EventArgs e)        
        {
            int bStatus;
            //if (checkValidate() == true)
            //{
                int PreFix = Convert.ToInt32(cbPrefix.SelectedValue);
                string strName = txtName.Text;
                string strSerName = txtSerName.Text;
                //if (rbExperson.Checked == true)
                //{ cbEmployeeList.SelectedValue = ""; }                
                string strAddress = txtAddress.Text;
                string strPostCode = txtPostCode.Text;
                string strTel = txtTelNum.Text;                
                if (cboxStatus.Checked == true)
                { bStatus = 1; }
                else
                { bStatus = 0; }
                int ActiveCheck = bStatus;
                int UserID = ssMng.EmpSeqI;
                DateTime dtNow = DateTime.Now;
                UpdateUI("Save");
                TrainingLecturersInfo info = new TrainingLecturersInfo();
                info.citizen = txtCitizen.Text.Trim();
                info.name_prefix_seq = PreFix;
                info.first_name_local = strName;
                info.last_name_local = strSerName;
                info.address = strAddress;
                info.district_seq = cn.ConvertNullInt(cmbDistrict.SelectedValue);
                info.prov_seq = cn.ConvertNullInt(cmbProvince.SelectedValue);
                info.tel = strTel;
                info.postal_code = strPostCode;
                info.email = txtEmail.Text;
                info.website = txtWebSite.Text;
                info.active = ActiveCheck;                
                info.created_by = UserID;
                info.created_when = dtNow;
                //info.amp_seq = cn.ConvertNullInt("");
                //info.emp_seq = cn.ConvertNullInt(cbEmployeeList.SelectedValue);

                LecturersBLL.Insert(info);

                grdTRLecturers.DataSource = LecturersBLL.GetFull();
                grdTRLecturers.DataBind();
            //}
        }
        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            int bStatus;
            int PreFix = Convert.ToInt32(cbPrefix.SelectedValue);
            string strName = txtName.Text == "" ? null : txtName.Text;
            string strSerName = txtSerName.Text == "" ? null : txtSerName.Text;
           // if (rbExperson.Checked == true)
            //{ cbEmployeeList.SelectedValue = ""; }
            string strAddress = txtAddress.Text == "" ? null : txtAddress.Text;
            string strPostCode = txtPostCode.Text == "" ? null : txtPostCode.Text;
            string strTel = txtTelNum.Text == "" ? null : txtTelNum.Text;
            if (cboxStatus.Checked == true)
            { bStatus = 1; }
            else
            { bStatus = 0; }
            int ActiveCheck = bStatus;
            int UserID = ssMng.EmpSeqI;
            DateTime dtNow = DateTime.Now;
            
            TrainingLecturersInfo info = new TrainingLecturersInfo();
            info.trainer_seq = Convert.ToInt32(txtTrainerSeq.Text);
            info.name_prefix_seq = PreFix;
            info.first_name_local = strName;
            info.last_name_local = strSerName;
            //info.emp_seq = cn.ConvertNullInt(cbEmployeeList.SelectedValue);
            info.address = strAddress;
            info.postal_code = strPostCode;
            info.tel = strTel;
            info.citizen = txtCitizen.Text.Trim();
            info.email = txtEmail.Text == "" ? null : txtEmail.Text;
            info.website = txtWebSite.Text == "" ? null: txtWebSite.Text;
            //info.district_seq = cn.ConvertNullInt(cmbDistrict.SelectedValue);
            info.district_seq = cmbDistrict.SelectedValue == "&nbsp;" ? null : cn.ConvertNullInt(cmbDistrict.SelectedValue);
            //info.amp_seq = cn.ConvertNullInt();
            //info.prov_seq = cn.ConvertNullInt(cmbProvince.SelectedValue);
            info.prov_seq = cmbProvince.SelectedValue == "&nbsp;" ? null : cn.ConvertNullInt(cmbProvince.SelectedValue);
            info.active = ActiveCheck;
            info.created_by = UserID;
            info.created_when = dtNow;

            LecturersBLL.Update(info);

            //LoadDataGrid();
            UpdateUI("Save");

            grdTRLecturers.DataSource = LecturersBLL.GetFull();
            grdTRLecturers.Rebind();

            ClearControl();
        }
        protected void btnCancel_Click(object sender, EventArgs e)
        {
            UpdateUI("Save");
            ClearControl();
            grdTRLecturers.DataSource = LecturersBLL.GetFull();
            grdTRLecturers.DataBind();
        }
        //protected void rbInperson_CheckedChanged(object sender, EventArgs e)
        //{
        //    if (rbInperson.Checked == true)
        //    {
        //        rbExperson.Checked = false;
        //        AtiveEmployee.Visible = true;
        //    }
        //}
        //protected void rbExperson_CheckedChanged(object sender, EventArgs e)
        //{
        //    if (rbExperson.Checked == true)
        //    {
        //        rbInperson.Checked = false;
        //        cbEmployeeList.Items.Clear();
        //        cbEmployeeList.Text = "";
        //        AtiveEmployee.Visible = false;                
        //    }

        //}

        //================================ Load Combobox =================================

        protected void LoadPrefix()
        {
            IList<MasNamePrefixInfo> infoList = NamePreBLL.ListNamePrefixes();
            cbPrefix.DataSource = infoList;
            cbPrefix.DataBind();
        }
        protected void cmbDistrict_ItemsRequested(object o, RadComboBoxItemsRequestedEventArgs e)
        {
            LoadDistrict("", cmbProvince.SelectedValue);
        }
        protected void LoadProvince()
        {
            // IList<MasProvinceInfo> infoList = proBLL.ListMasProvinces();
            DataSet ds;
            DataTable dt;
            ds = proBLL.LoadMasProvinces((int?)null, (int?)null);
            dt = ds.Tables[0];

            cmbProvince.DataSource = dt;
            cmbProvince.DataBind();
        }
        protected void LoadDistrict(string Amphur, string Province)
        {
            IList<MasDistrictInfo> infoList = disBLL.ListDistricts("", "", Province);
            IList<MasDistrictInfo> List = new List<MasDistrictInfo>();

            for (int i = 0; i < infoList.Count; i++)
            {
                MasDistrictInfo c = new MasDistrictInfo();
                c.Sequence = infoList[i].Sequence;
                c.Name = "ตำบล" + infoList[i].Name + " อำเภอ" + infoList[i].Amphurs.Name;

                List.Add(c);
            }
            cmbDistrict.DataSource = List;
            cmbDistrict.DataBind();
        }

        //protected void LoadEmployeeList()
        //{
        //    IList<EmployeeInfo> infoList = EmpBLL.ListEmployees();

        //    foreach (EmployeeInfo List in infoList)
        //    {
        //        RadComboBoxItem item = new RadComboBoxItem();
        //        item.Text = List.Person.FirstName + " " + List.Person.LastName;
        //        item.Value = List.Sequence.ToString();

        //        item.Attributes.Add("EMP_SEQ", List.Sequence.ToString());
        //        item.Attributes.Add("EMP_NAME", List.Person.FirstName + " " + List.Person.LastName);


        //        cbEmployeeList.Items.Add(item);
        //        item.DataBind();
        //    }
        //}
        //=======================================================================

        //protected bool checkValidate()
        //{
        //    bool bOK = true;
        //    string strPrefix = cbPrefix.SelectedValue;
        //    string strName = (txtName.Text).ToString();
        //    string strSerName = (txtSerName.Text).ToString();
        //    string strExperson = cbEmployeeList.SelectedValue;
        //    if (strPrefix == "")
        //    {
        //        bOK = false;
        //        lblError.Text = "* กรุณาเลือกคำนำหน้าชื่อ";
        //    }            
        //    else if (strName.Length == 0)
        //    {
        //        bOK = false;
        //        lblError.Text = "* กรุณาใส่ชื่อวิทยากร";
        //    }
        //    else if (strSerName.Length == 0)
        //    {
        //        bOK = false;
        //        lblError.Text = "* กรุณาใส่นามสกุล";
        //    }
        //    return bOK;
        //}

        protected void ClearControl()
        {
            //lblError.Text = "";
            cboxStatus.Checked = true;
            //rbExperson.Checked = true;
            //cbPrefix.Items.Clear();
            //cbPrefix.Text = "";

            //cmbProvince.Items.Clear();
            //cmbProvince.Text = "";

            //cmbAmphurs.Items.Clear();
            //cmbAmphurs.Text = "";
            //cmbDistrict.Items.Clear();
            //cmbDistrict.Text = "";

            //cbEmployeeList.Items.Clear();
            //cbEmployeeList.Text = "";

            txtCitizen.Text = string.Empty;
            txtName.Text = string.Empty;
            txtSerName.Text = string.Empty;
            txtAddress.Text = string.Empty;
            txtPostCode.Text = string.Empty;
            txtTelNum.Text = string.Empty;
            txtEmail.Text = string.Empty;
            txtWebSite.Text = string.Empty;
            cboxStatus.Checked = false;

        }
        protected void UpdateUI(string strCommand)
        {
            switch (strCommand)
            {
                case "Save":
                    LecturersEdit.Visible = false;
                    tblTRLecturers.Visible = true;                    

                    break;

                case "AddNew":
                    LecturersEdit.Visible = true;
                    btnSave.Visible = true;
                    btnUpdate.Visible = false;
                    tblTRLecturers.Visible = false;

                    break;

                case "Update":
                    LecturersEdit.Visible = true;
                    btnSave.Visible = false;
                    btnUpdate.Visible = true;
                    tblTRLecturers.Visible = false;

                    break;
            }
        }
    }
}
